package cn.yfj.pet.service.impl;

import cn.yfj.basic.util.BaiduAuditUtils;
import cn.yfj.basic.util.StrUtils;
import cn.yfj.org.domain.Employee;
import cn.yfj.pet.domain.Pet;
import cn.yfj.pet.domain.PetDetail;
import cn.yfj.pet.domain.PetOnlineAuditLog;
import cn.yfj.pet.mapper.PetDetailMapper;
import cn.yfj.pet.mapper.PetMapper;
import cn.yfj.pet.mapper.PetOnlineAuditLogMapper;
import cn.yfj.pet.service.IPetService;
import cn.yfj.basic.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yfj
 * @since 2022-02-24
 */
@Service
@Slf4j
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Override
    public void onsale(List<Long> ids) {
        //审核上架的宠物信息是否合法
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            String name = pet.getName();
            StringBuilder note = new StringBuilder();
            //使用百度ai
            if (!BaiduAuditUtils.TextCensor(name)){
                note.append("名字不合法哦");
            }
/*            //审核图片
            String resources = pet.getResources();
            if (StringUtils.isEmpty(resources)){
                //分割多个图片路径
                String[] split = resources.split(",");
                if (BaiduAuditUtils.ImgCensor("http://47.95.117.210:8888"+split[0]))){
                    note.append("图片不合法");
                }
            }*/
            //判断审核是否成功
            if (note.length()>0){
                //不成功状况
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0); //下架状态
                log.setAuditTime(new Date());
                log.setNote(note.toString());
                petOnlineAuditLogMapper.save(log);
            }else {
                //成功状况
                HashMap<String, Object> map = new HashMap<>();
                map.put("id",id); //进行上架
                map.put("onSaleTime",new Date());
                petMapper.onSale(map);
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        //成功状况
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids); //进行下架
        map.put("onSaleTime",new Date());
        petMapper.offSale(map);
    }

    @Override
    @Transactional
    public void addPetAndDetail(Pet pet, Employee login) {
        //保存宠物信息
        System.out.println(pet);
        petMapper.save(pet);

        initPet(pet,login.getShop_id());
        //保存宠物详情信息
        PetDetail detail = pet.getDetail();
        if (detail!=null){
            //保存宠物id关联
            detail.setPetId(pet.getId());
            petDetailMapper.save(pet.getDetail());
        }
    }

    private void initPet(Pet pet, Long shop_id) {
        pet.setShopId(shop_id);
    }
}
